<?php

namespace Core;

require_once "_root.php";

final class Main {

    private static $objInstance;

    // Public read only members.
    private $_Front;
    private $_Paths; // Q: Why isn't this public yet?

    private static $strCorePath;

    /**
     * Gets an instance of itself.
     *
     * @return Core\Controllers\Front
     */
    public static function getInstance () {

        if ( !self::$objInstance instanceof self )
            self::$objInstance = new self;

        return self::$objInstance;

    }

    function __construct () {
        $this->_Front = Controllers\Front::getInstance();
        $this->_Paths = Paths::getInstance();
    }

    function __get ( $strMemberName ) {

        switch ( $strMemberName ) {
            case "Front":
                return $this->_Front;
        }

    }

    /**
     * Autoloads classes.
     *
     * @param string $strClass
     */
    public static function autoload ( $strClass ) {

        $arrNamespaceSegments = explode( "\\", $strClass );
        $strClassName = array_pop( $arrNamespaceSegments );

        if ( $arrNamespaceSegments ) {

            switch ( $arrNamespaceSegments[0] ) {

                case "Core":

                    // Remove the Core segment.
                    array_shift( $arrNamespaceSegments );

                    // Load, if present, the _root.php for this namespace.
                    $strRootFilename = self::getCorePath() . "/" . implode( "/", $arrNamespaceSegments ) . "/_root.php";
                    if ( file_exists( $strRootFilename ) ) {
                        require_once $strRootFilename;
                    }

                    // Load the file we hope contains the class we're after.
                    $strFilename = str_replace( "\\", "/", substr( $strClass, 5 ) . ".php" ); // 'Core\' is removed from $strClass.
                    require_once $strFilename;

                    break;

                case "Models":
                    $Paths = Paths::getInstance();
                    require_once $Paths->Models . substr(  );
                    break;

                default:

            }

        }

    }

    /**
     * Shortcut for instance->Front->process()
     */
    public function process () {
        $this->Front->process();
    }

    /**
     * Gets the path were the Core Framework is located.
     *
     * @return string
     */
    private static function getCorePath () {

        if ( self::$strCorePath ) {
            return self::$strCorePath;
        } else {

            $arrDocumentRoot = explode( "/", $_SERVER['DOCUMENT_ROOT'] );
            $arrFilePath = explode( "/", __FILE__, -1 );
            $intBelow = count( array_diff_assoc( $arrDocumentRoot, $arrFilePath ) );

            $arrLibraryPath = array_diff_assoc( $arrFilePath, $arrDocumentRoot );

            for ( $i = 0; $i < $intBelow; $i++ ) {
                array_unshift( $arrLibraryPath, ".." );
            }

            $strLibraryPath = implode( "/", $arrLibraryPath );
            return $strLibraryPath;

        }

    }

    public function info () {
        
    }

}


//        set_error_handler( function ( $errno, $errstr, $errfile, $errline ) {
//            echo "Hiii!";
//            throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
//        }, E_ALL | E_STRICT );

?>